using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DATalonarios
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region DATalonariosGetAll
        /// <summary>
        /// Devuelve lista de Talonarios de Ventas
        /// </summary>
        /// <param name="Connection"></param>
        /// <returns><List>Talonarios</List></returns>
        public List<Talonarios> DATalonariosGetAll(string Connection)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("Talonarios_GetAll");

                List<Talonarios> listTalonarios = new List<Talonarios>();
                using (cmd)
                {
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Talonarios oTalonario = new Talonarios();

                        oTalonario.Id = (int)Odbreader["Id"];
                        oTalonario.Nombre = (string)Odbreader["Nombre"];
                        oTalonario.Prefijo = (string)Odbreader["Prefijo"];
                        oTalonario.Tipo = (string)Odbreader["Tipo"];
                        oTalonario.De = (int)Odbreader["De"];
                        oTalonario.A = (int)Odbreader["A"];
                        int iTempH = Convert.ToInt16(Odbreader["Habilitado"].ToString());
                        if (iTempH == 1)
                        {
                            oTalonario.Habilitado = true;
                        }
                        else
                        {
                            oTalonario.Habilitado = false;
                        }
                        int iTempC = Convert.ToInt16(Odbreader["Control"].ToString());
                        if (iTempC == 1)
                        {
                            oTalonario.Control = true;
                        }
                        else
                        {
                            oTalonario.Control = false;
                        }
                        oTalonario.Mes = (int)Odbreader["Mes"];
                        oTalonario.Anio = (int)Odbreader["Anio"];

                        listTalonarios.Add(oTalonario);
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return listTalonarios;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DaTalonariosUpdate
        /// <summary>
        /// Actualiza un Talonario en base a su Id
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Talonarios"></param>
        public void DaTalonariosUpdate(string Connection, Talonarios talonario)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Talonarios_Update");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id", DbType.Int16, talonario.Id);
                    DataBase.AddInParameter(cmd, "@Nombre", DbType.String, talonario.Nombre);
                    DataBase.AddInParameter(cmd, "@Prefijo", DbType.String, talonario.Prefijo);
                    DataBase.AddInParameter(cmd, "@Tipo", DbType.String, talonario.Tipo);
                    DataBase.AddInParameter(cmd, "@De", DbType.Int16, talonario.De);
                    DataBase.AddInParameter(cmd, "@A", DbType.Int16, talonario.A);
                    DataBase.AddInParameter(cmd, "@Habilitado", DbType.Int16, talonario.Habilitado);
                    DataBase.AddInParameter(cmd, "@Control", DbType.Int16, talonario.Control);
                    DataBase.AddInParameter(cmd, "@Mes", DbType.Int16, talonario.Mes);
                    DataBase.AddInParameter(cmd, "@Anio", DbType.Int16, talonario.Anio);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}

